<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>页面</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">

</head>
<body>
<div id="app">
    <el-container v-loading="loading">
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">基础库</el-breadcrumb-item>
                <el-breadcrumb-item>规格库</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <el-header>

            <el-row >
                <!--<a href="/Users/10659/Desktop/新建文件夹%20(4)/user/userAdd.html">-->
                <el-col :xs="8" :sm="8" :md="4" :lg="2" :xl="2">
                    <el-button type="primary" icon="el-icon-edit" :offset="6" @click="add">新增</el-button>
                </el-col>
                <!--</a>-->
                <el-col :xs="14" :sm="14" :md="8" :lg="8" :xl="8">
                    <el-input placeholder="请输入内容" @keyup.enter.native="enterSearch" v-model="keySearch">
                        <el-button slot="append" icon="el-icon-search" @click=search></el-button>
                    </el-input>
                </el-col>
            </el-row>
        </el-header>
        <el-main>
            <el-table :data="tableData" stripe border style="width:100%" highlight-current-row size="mini" tooltip-effect="dark" height="calc(100vh - 240px)">
                　　
                <el-table-column label="序号" align="center" min-width="50">
                    　　<template scope="scope"><span>{{scope.$index+(currentPage - 1) * pageSize + 1}}</span></template>
                </el-table-column>
                　　
                <el-table-column prop="spec_name" label="名称" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                <el-table-column prop="slug_name_set" label="属性" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                　　
                <el-table-column label="操作" align="center" min-width="100" fixed="right">
                    　　　　
                    <template slot-scope="scope">
                        <el-button type="text"
                                   @click="modify(scope.row.id,scope.row.spec_name,scope.row.slug_name_set)">编辑
                        </el-button>
                        <el-button type="text" @click="remove(scope.row.id,scope.row.spec_name,scope.row.slug_name_set)">作废
                        </el-button>
                    </template>
                    　　
                </el-table-column>

            </el-table>
        </el-main>
        <el-footer>
            <el-row >
                <el-col >
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :page-sizes="[100, 500, 1000]" :page-size="pageSize"
                                   layout="total, sizes, prev, pager, next, jumper"
                                   :total="totalRecords" >
                    </el-pagination>
                </el-col>
            </el-row>
        </el-footer>
        <el-dialog title="请输入规格信息" :visible.sync="dialogFormVisible" @close="close">
            <el-form label-width="80px" class="demo-dynamic">
                <el-form-item prop="specName" label="规格">
                    <el-row >
                        <el-col :xs="14" :sm="14" :md="16" :lg="12" :xl="12">
                    <el-input v-model="specName" :disabled="isAble"></el-input>
                        </el-col>
                        <el-col :xs="2" :sm="2" :md="4" :lg="4" :xl="4">
                    <el-button @click="addDomain">新增属性</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item v-for="(domain, index) in specSlugSet" :label="'属性' + domain.sort"
                              :prop="'specSlugSet.' + index + '.slugName'">
                    <el-row >
                        <el-col :xs="14" :sm="14" :md="16" :lg="12" :xl="12">
                    <el-input v-model="domain.slugName" :disabled="domain.edit"></el-input>
                        </el-col>
                        <el-col :xs="2" :sm="2" :md="4" :lg="4" :xl="4">
                    <el-button @click.prevent="removeDomain(domain)" :disabled="domain.edit">删除</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </div>
        </el-dialog>
    </el-container>
</div>

<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script >
    new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                currentPage: 1, //当前页
                pageSize: 100, //每页总数
                totalRecords: 0, //总条数
                keySearch: '',
                dialogFormVisible: false,
                id: 0,
                specName: '',
                specSlugSet: [],
                loading:false,
                isAble:false,
            };
            show: false

        },
        methods: {
            close(){
                this.specName = '';
                this.id = '';
                this.specSlugSet=[];
                this.isAble=false;
            },
            removeDomain(item) {
                var index = this.specSlugSet.indexOf(item)
                if (index !== -1) {
                    this.specSlugSet.splice(index, 1)
                }
            },
            addDomain() {
                console.log(this.specSlugSet)
                this.specSlugSet.unshift({
                    slugName: '',
                    sort:this.specSlugSet.length+1
                });
                console.log(this.specSlugSet)
            },
            //打开新增弹窗
            add() {
                this.id=0;
                this.dialogFormVisible = true;
            },
            //状态转换
            formatStatus: function (row, column) {
                return row.brand_status === '00' ? '有效' : row.brand_status === '99' ? '无效' : '未知';
            },
            //回车监听事件获取输入内容
            enterSearch(event) {
                this.currentPage=1;
                this.pageSize=100;
                this.keySearch = event.target.value;
                this.ajaxFunction()
            },
            //点击事件获取输入内容
            search() {
                this.currentPage=1;
                this.pageSize=100;
                this.ajaxFunction()
            },
            //作废
            remove(id,spec_name,slug_name_set) {
                this.$confirm('此操作将永久作废该规格, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    $.ajax({
                        url: '/dataCenter/specEdit.json',
                        type: 'post',
                        data: {
                            id: id,
                            spec_name: spec_name,
                            spec_slug: slug_name_set,
                            type:"cancel"
                        },
                        dataType: 'json',
                        success: (result) =>{
                            if (result.ret) {
                                this.ajaxFunction();
                                this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                            } else {
                                this.$message.error('操作失败,请稍后重试')
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            //打开修改弹窗
            modify(id,spec_name,slug_name_set) {
                this.isAble=true;
                this.specName = spec_name;
                this.id = id;
                // let strs = slug_name_set.split(",");
                // var specSlugSet = [];
                // strs.forEach((element) => {
                //     specSlugSet.push({
                //         slugName: element
                //     })
                // })
                // this.specSlugSet =specSlugSet;
                $.ajax({
                    url: '/dataCenter/SlugListBySpecId.json?specId='+id,
                    type: 'get',
                    dataType: 'json',
                    success: (result) =>{
                        console.log(result);
                        if (result.ret) {
                            this.specSlugSet = result.data;
                            this.specSlugSet.forEach((element,index) => {
                                element.slugName = element.slug_name;
                                element.edit = true;
                                element.sort = (index+1);
                            })
                            this.specSlugSet.reverse()
                            this.dialogFormVisible = true;
                        }else{
                            this.$message.error('加载失败，请稍后重试');
                        }
                    }
                });
            },
            //取消
            cancel() {
                this.specName = '';
                this.id = '';
                this.specSlugSet=[];
                this.dialogFormVisible = false;
            },
            //提交
            submit() {
                if(this.specSlugSet.length === 0){
                    this.$message.error('请至少输入一个属性值');
                }else{
                    this.loading=true;
                    this.dialogFormVisible = false;
                    if (0 === this.id) {
                        $.ajax({
                            url: '/dataCenter/specEdit.json',
                            type: 'post',
                            data: {
                                id: this.id,
                                spec_name: this.specName,
                                spec_slug: JSON.stringify(this.specSlugSet),
                                type:"add"
                            },
                            dataType: 'json',
                            // async:false,
                            success: (result) =>{
                                this.loading=false;
                                this.isAble=false;
                                if(result.ret){
                                    this.$message({
                                        type: 'success',
                                        message: '操作成功!'
                                    });
                                    this.ajaxFunction()
                                }else{
                                    this.$message.error("操作失败，请稍后重试");
                                }
                            }
                        });
                    } else {
                        $.ajax({
                            url: '/dataCenter/specEdit.json',
                            type: 'post',
                            data: {
                                id: this.id,
                                spec_name: this.specName,
                                spec_slug: JSON.stringify(this.specSlugSet),
                                type:"update"
                            },
                            dataType: 'json',
                            success: (result)=> {
                                this.loading=false;
                                this.isAble=false;
                                if(result.ret){
                                    this.$message({
                                        type: 'success',
                                        message: '操作成功!'
                                    });
                                    this.ajaxFunction()
                                }else{
                                    this.$message.error("操作失败，请稍后重试");
                                }
                            }
                        });
                    }
                }

            },
            async handleSizeChange(val) {
                this.pageSize = val;
                this.ajaxFunction()
            },
            //
            async handleCurrentChange(val) {

                this.currentPage = val;
                this.ajaxFunction()
            },
            ajaxFunction() {
                $.ajax({
                    url: '/dataCenter/specList.json?param='+this.keySearch+'&page='+this.currentPage+'&limit='+this.pageSize,
                    type: 'get',
                    dataType: 'json',
                    success: (result) =>{
                        if (result.ret) {
                            this.tableData = result.data;
                            this.totalRecords = result.count;
                            this.specSlugSet=[];
                            this.specName = '';
                            this.id = 0;
                        }else{
                            this.$message.error('加载失败，请稍后重试');
                        }
                    }
                });
            },
        },
        mounted() {
            this.ajaxFunction();
        },
    })
</script>

</body>
</html>

